library FireMission initializer init requires LegacySystem

	globals
		private unit U
	endglobals
	private function Trig_Artillery_Conditions takes nothing returns boolean
		return GetSpellAbilityId() == 'A08W' or GetSpellAbilityId() == 'S003'
	endfunction

	// creates trueshot aura effect
	private function createTarget takes unit caster, real x, real y, real barrage_delay returns unit
		local player p = GetOwningPlayer(caster)
		local unit u 
		
		//DangerZone Dummy
		set u  = CreateUnit(Player(14),'n00P',x,y,0.0)
		call UnitApplyTimedLife(u ,'Bhwd', barrage_delay + 6.0)
		//trueshot aura
		set u = CreateUnit( p , 'e005', x, y, 0.00 )
		call UnitApplyTimedLife(u ,'Bhwd',barrage_delay + 6.0)
		call PingMinimapEx(x,y,2,255,255,255,false)
		set U = u
		set p = null
		set u = null
		return U
	endfunction

	private function playArtillerySound takes unit origin returns nothing
		call AttachSoundToUnit(gg_snd_Artillery,origin)
		call SetSoundVolume(gg_snd_Artillery,127)
		call StartSound(gg_snd_Artillery)
	endfunction

	private function GetArtilleryDamage takes integer spell_level returns integer
		// done with if statements incase a non-linear pattern of damage is desired in the future
		local integer damage
		if(spell_level == 0) then
			set damage = 400
		elseif(spell_level == 1) then
			set damage = 400
		elseif(spell_level == 2) then
			set damage = 550
		elseif(spell_level == 3) then
			set damage = 700
		elseif(spell_level == 4) then
			set damage = 850
		else 
			set damage = 1000
		endif
		return damage
	endfunction



	private function Trig_Artillery_Actions takes nothing returns nothing
		
		// variables
		local unit caster = GetTriggerUnit()
		local integer spell_level= GetUnitAbilityLevelSwapped(GetSpellAbilityId(), caster)
		local real x = GetSpellTargetX()
		local real y = GetSpellTargetY()
		local integer total_shells = getInteger(null,"FO_total_shells")
		local integer dp = getInteger(null,"FO_dp") - 50
		local integer number_of_shells = getInteger(null,"FO_number_of_shells")
		local real barrage_delay = I2R(getInteger(null,"FO_barrage_delay") )
		local integer damage = GetArtilleryDamage(spell_level)
		local unit target_aura

		// core
		// check for shells and substract number per barrage
		if(number_of_shells > total_shells) then 
			 call print ("Shells depleted")  
			 set target_aura = null
			 set caster = null
			 return
		else
			 call putInteger(null,"FO_total_shells", total_shells - number_of_shells)
			 call print("Shells Remaining: " + I2S(total_shells - number_of_shells))
		endif 

		// create artillery beacon
		set target_aura = createTarget(caster,x, y, barrage_delay)   

		call PolledWait(barrage_delay-2.0)
	   
		// play sound 2 seconds before impact
		call playArtillerySound(target_aura)

		call PolledWait(2.0)    

		call EarShockArea(x, y, 500)

	    call ArtHandler(caster,x,y,number_of_shells, damage, dp, 0.1 , 1)
		 
		call PolledWait(15.0)
		// cleanup
		set target_aura = null
		set caster = null
	endfunction

	private  function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition( t, Condition( function Trig_Artillery_Conditions ) )
		call TriggerAddAction( t, function Trig_Artillery_Actions )
	endfunction
	
endlibrary